<?php

class Costumer extends CI_Model {

    function index($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('costumer');
        return $q;
    }

    function create($name, $username, $email, $password, $last_name, $address, $home_phone, $mobile_phone, $country, $city, $state, $zip_code, $user_confirmation, $link, $date) {
        $data = array(
            'name' => $name,
            'username' => $username,
            'email' => $email,
            'password' => $password,
            'last_name' => $last_name,
            'address' => $address,
            'home_phone' => $home_phone,
            'mobile_phone' => $mobile_phone,
            'country' => $country,
            'city' => $city,
            'state' => $state,
            'zip_code' => $zip_code,
            'link' => $link,
            'user_confirmation' => $user_confirmation,
            'date_created' => $date,
        );

        $this->db->insert('costumer', $data);

        return true;
    }

//    function duplicate($username) {
//        $this->db->where('username');
//        $query = $this->db->get('costumer');
//        if ($query->num_rows() == 0) {
//            return true;
//        }else{
//            redirect('login/create/duplicate');
//            exit;
//        }
//        
//    }

    function update($id, $name, $username, $email, $last_name, $address, $home_phone, $mobile_phone, $country, $city, $state, $zip_code, $date) {
        $data = array(
            'name' => $name,
            'username' => $username,
            'email' => $email,
            'last_name' => $last_name,
            'address' => $address,
            'home_phone' => $home_phone,
            'mobile_phone' => $mobile_phone,
            'country' => $country,
            'city' => $city,
            'state' => $state,
            'zip_code' => $zip_code,
            'date_created' => $date,
        );
        $this->db->where('id', $id);
        $this->db->update('costumer', $data);
        redirect('costumers/index');
    }

    function login($username, $password) {
        $this->db->select('id, username, password, status, user_confirmation');
        $this->db->from('costumer');
        $this->db->where('username = ' . "'" . $username . "'");
        $this->db->where('password = ' . "'" . md5($password) . "'");
        $this->db->limit(1);

        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }

    function chek_confirmation($link) {
        $this->db->where('link', $link);
        $q = $this->db->get('costumer');
        foreach ($q->result() as $row) {
            $data = $row->user_confirmation;
        }
        if ($q->result() == null) {
            return FALSE;
        } {
            return $data;
        }
    }

    function user_confirm($link, $confirm) {
        $data = array(
            'user_confirmation' => $confirm
        );
        $this->db->where('link', $link);
        $this->db->update('costumer', $data);
        redirect('confirm/success');
    }

    function booking_temp($id_user, $data) {
        
    }

    function booking_create($data) {
        $this->db->insert('booking', $data);
        return true;
    }

    function booking_create_adult($data) {
        $this->db->insert('booking_adult', $data);
        return true;
    }

    function booking_create_child($data) {
        $this->db->insert('booking_child', $data);
        return true;
    }

    function booking_create_infant($data) {
        $this->db->insert('booking_infant', $data);
        return true;
    }

    function status_book($id_user) {
        $this->db->where('id_user', $id_user);
        $q = $this->db->get('booking');
        return $q->result();
    }

    public function book_hitung($id_user) {
        $this->db->where('id_user', $id_user);
        $q = $this->db->get("booking");
        $data = count($q->result());
        return $data;
    }

    public function issued_hitung($id_user) {
        $this->db->where(array('id_user' => $id_user, 'status' => '3'));
        $q = $this->db->get("booking");
        $data = count($q->result());
        return $data;
    }

    public function record_book($id_user) {
        $this->db->where('id_user', $id_user);
        return $this->db->count_all("booking");
    }

    function fetch_book($limit, $start, $id_user) {
        $this->db->where('id_user', $id_user);
        $this->db->order_by('id', 'DESC');
        $this->db->limit($limit, $start);
        $query = $this->db->get("booking");
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function record_issued($id_user) {
        $this->db->where(array('id_user' => $id_user, 'status' => '1'));
        return $this->db->count_all("booking");
    }

    function fetch_issued($limit, $start, $id_user) {
        $this->db->where(array('id_user' => $id_user));
        $this->db->order_by('id', 'DESC');
        $this->db->limit($limit, $start);
        $query = $this->db->get("booking");
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    function conissued($id, $id_user) {
        $this->db->where(array('id' => $id, 'id_user' => $id_user));
        $q = $this->db->get('booking');
        return $q->result();
    }

    function show_book($id, $id_user) {
        if (strlen($id) == 6)
            $this->db->where(array('code' => $id, 'id_user' => $id_user));
        else
            $this->db->where(array('id' => $id, 'id_user' => $id_user));

        $q = $this->db->get('booking');
        return $q->result();
    }

    function confirmbook($id_book, $id_costumer, $total) {
        $data = array(
            'issued' => 1,
        );

        $this->db->where(array('id' => $id_book, 'id_user' => $id_costumer));
        $q = $this->db->update('booking', $data);
        return true;
    }

    function confirmissue($id_costumer, $total) {
        $data = array(
            'costumer_id' => $id_costumer,
            'credit' => $total,
        );
        $q = $this->db->insert('deposit', $data);
        $notif = "Issue telah berhasil";
        redirect('costumers/status_issued/Success');
    }

    function check_e($email) {
        $this->db->where('email', $email);
        $q = $this->db->get('costumer');
        $data = count($q->result());
        return $data;
    }

    function sendpassword($email, $date, $link) {
        $data = array(
            'email' => $email,
            'link' => $link,
            'date' => $date,
        );
        $this->db->insert('costumerf', $data);
        redirect('login/costumer');
    }

    function check_link($link) {
        $this->db->where('link', $link);
        $q = $this->db->get('costumerf');
        return $q->result();
    }

    function costumer_password($email, $password) {
        $this->db->where('email', $email);
        $data = array('password' => $password);
        $this->db->update('costumer', $data);
        redirect('login/resetfinish');
    }

    function get_token_date() {
        $q = $this->db->get('token_api');

        foreach ($q->result() as $token) {
            return $date_t = $token->date;
        }
    }

    function get_token() {
        $q = $this->db->get('token_api');

        foreach ($q->result() as $token) {
            return $date_t = $token->token;
        }
    }

    function update_token($date, $token) {
        $jsonfile = "https://api.master18.tiket.com/apiv1/payexpress?method=getToken&secretkey=a31f3350fd4f01039d33037f972183fc&output=json";
        $data = json_decode(file_get_contents($jsonfile), true);
        $token1 = $data['token'];

        $value = array(
            'token' => $token1,
            'date' => $date
        );

        $this->db->update('token_api', $value);
        return true;
    }

    function get_new_token() {
        $q = $this->db->get('token_api');
        foreach ($q->result() as $row) {
            return $row->token;
        }
    }

    function get_costumer($date) {
        $this->db->like('expire', $date);
        $q = $this->db->get('costumer');
        return $q->result();
    }

    function expire_tambah($id, $expire) {
        $this->db->where('id', $id);
        $data = array('expire' => $expire);
        $this->db->update('costumer', $data);
        return true;
    }

    function get_email($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('costumer');
        foreach ($q->result() as $row):
            return $row->email;
        endforeach;
    }
    
    function cek_username($username){
        $this->db->where('username', $username);
        $q = $this->db->get('costumer');
        return $q->result_array();
    }
    
    function cek_email($email){
        $this->db->where('email', $email);
        $q = $this->db->get('costumer');
        return $q->result_array();
    }
    
    function get_expire($id){
        $this->db->where('id', $id);
        $q = $this->db->get('costumer');
        foreach($q->result() as $row){
            return $row->expire;
        }
    }

}

?>